package com.minmin.common;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.concurrent.TimeUnit;

@Slf4j
public class Driver {

    private WebDriver driver;

    public Driver(DriverConfig config) {
//        System.setProperty("webdriver.chrome.whitelistedIps", "");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");

        driver = new ChromeDriver(options);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        log.info("程序启动配置");

        log.info("打开网页[{}]", config.getUrl());
        driver.get(config.getUrl());
        driver.manage().window().maximize();

        log.info("登录账号");
        eXpath("(//button[@type=\"button\"])[2]").click();

        eXpath("//input[@placeholder=\"请输入用户名\"]").sendKeys(config.getUsername());
        eXpath("//input[@placeholder=\"请输入密码\"]").sendKeys(config.getPassword());

        driver.findElement(By.cssSelector("#app > div > div > div > button")).click();
        eXpath(config.getShopName()).click();


        log.info("登录成功");

        eXpath("/html/body/div[1]/section/aside/div/div[3]/i").click();
        eXpath("//span[text()=\"弹出式菜单\"]").click();

        this.driver = driver;
    }

    public WebElement eXpath(String x) {
        return driver.findElement(By.xpath(x));
    }

    public WebDriver getDriver() {
        return driver;
    }
}
